package com.potenzacd.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenzacd.project.mapper.ProductInfoMapper;
import com.potenzacd.project.pojo.ProductApprovalInfo;
import com.potenzacd.project.pojo.ProductInfo;
import com.potenzacd.project.pojo.ProductInfoTerminalType;
import com.potenzacd.project.pojo.ProjectInfo;
import com.potenzacd.project.service.IProductApprovalInfoService;
import com.potenzacd.project.service.IProductInfoService;
import com.potenzacd.project.service.IProductInfoTerminalTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cuidongdong
 * @since 2021-01-18
 */
@Service
public class ProductInfoService extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {
    @Autowired
    private IProductInfoTerminalTypeService productInfoTerminalTypeService;

    @Autowired
    private IProductApprovalInfoService productApprovalInfoService;

    @Autowired
    private ProductInfoService ProductInfoService;

    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Override
    public void save4draft(ProductInfo info) {
        // 维护
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        //保存基本信息
        this.save(info);
        //维护关系
        //终端
        //产品信息的标识
        String code = info.getCode();
        //获取终端信息
        List<ProductInfoTerminalType> productInfoTerminalTypes = info.getProductInfoTerminalTypes();
        //遍历
        for (ProductInfoTerminalType productInfoTerminalType: productInfoTerminalTypes) {
            //外键唯一标识
            productInfoTerminalType.setProductInfoCode(code);
            //入库 一个一个加入
            this.productInfoTerminalTypeService.save(productInfoTerminalType);
        }

    }

    @Override
    public void edit4Approval(ProductInfo info) {
        //维护属性
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        //保存基本信息
        this.save(info);
        //产品信息
        String code = info.getCode();
        List<ProductInfoTerminalType> productInfoTerminalTypes = info.getProductInfoTerminalTypes();
        for (ProductInfoTerminalType productInfoTerminalType:productInfoTerminalTypes){
            //给外键赋值
            productInfoTerminalType.setProductInfoCode(code);
            this.productInfoTerminalTypeService.save(productInfoTerminalType);

        }
    }

    @Override
    public void edit4draft(ProductInfo info) {
        //维护属性
        info.setUpdateTime(new Date());
        //基本信息更新
        this.updateById(info);
        //处理产品的终端类型信息 删除原来的信息添加新的信息
        String code = info.getCode();
        //构造条件查询
        QueryWrapper<ProductInfoTerminalType> terminalTypeMapperQueryWrapper = new QueryWrapper<>();
        //设置传入的code与查询的code是否相等
        terminalTypeMapperQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,code);
        this.productInfoTerminalTypeService.remove(terminalTypeMapperQueryWrapper);
        //更新产品中终端的信息&
        this.productInfoTerminalTypeService.saveBatch(info.getProductInfoTerminalTypes());
        //状态
        info.setStatus(ProductInfo.Status.STATUS_DRAFT);

    }

    @Override
    public void save4Approval(ProductInfo info) {
        //维护属性
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        //保存基本信息
        this.save(info);
        //产品信息
        String code = info.getCode();
        List<ProductInfoTerminalType> productInfoTerminalTypes = info.getProductInfoTerminalTypes();
        for (ProductInfoTerminalType productInfoTerminalType:productInfoTerminalTypes){
            //给外键赋值
            productInfoTerminalType.setProductInfoCode(code);
            this.productInfoTerminalTypeService.save(productInfoTerminalType);

        }
    }

    @Override
    public void approval(ProductApprovalInfo info) {
        //维护属性
        info.setCreateTime(new Date());
        //类型基本信息=1
        info.setType(1);
        //保存
        this.productApprovalInfoService.save(info);
        ProductInfo productInfo = new ProductInfo();
        //pass
        Integer pass = info.getPass();
        //pass=1  3通过  4不通过
        productInfo.setStatus(pass==1?ProductInfo.Status.STATUS_APPROVAL_PASS:ProductInfo.Status.STATUS_APPROVAL_FAIL);

    }

    @Override
    public void publish(ProductInfo info) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setStatus(ProductInfo.Status.STATUS_PUBLISH);
        this.updateById(productInfo);
    }

    @Override
    public void re4Approval(ProductInfo info) {
        //维护属性
        info.setUpdateTime(new Date());
        //基本信息更新
        this.updateById(info);
        //处理产品的终端类型信息 删除原来的信息添加新的信息
        String code = info.getCode();
        //构造条件查询
        QueryWrapper<ProductInfoTerminalType> terminalTypeMapperQueryWrapper = new QueryWrapper<>();
        //设置传入的code与查询的code是否相等
        terminalTypeMapperQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,code);
        this.productInfoTerminalTypeService.remove(terminalTypeMapperQueryWrapper);
        //更新产品中终端的信息&
        this.productInfoTerminalTypeService.saveBatch(info.getProductInfoTerminalTypes());
        //状态
        info.setStatus(ProductInfo.Status.STATUS_DRAFT);
    }

    @Override
    public void abort(ProductInfo info) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setStatus(ProductInfo.Status.STATUS_ABORT);
        this.updateById(productInfo);
    }

    @Override
    public List<ProductInfo> finData() {
        QueryWrapper<ProjectInfo> projectInfoQueryWrapper = new QueryWrapper<>();
        List<ProductInfo> productInfos = productInfoMapper.selectList(null);

        return productInfos;
    }


    @Override
    public ProductInfo detai(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);

        ProductInfo productInfo = ProductInfoService.getOne(productInfoQueryWrapper);


        return productInfo;
    }
}
